//
//  main.c
//  tcmalloctest
//
//  Created by Moe Suzuki on 11/06/28.
//  Copyright 2011年 Moe Suzuki All rights reserved.
//

#include <stdio.h>
#include <sys/time.h>

#include <stdio.h>

#define USE_DL_PREFIX
#define MSPACES 1
#include "malloc.c"

#include <google/tcmalloc.h>

int main (int argc, const char * argv[])
{
    const int tmax = 1024 * 16;
    const int packet = 64;
    unsigned char * tables[tmax];
    
    memset(tables,0,sizeof(tables));
    struct timeval tvs,tve;

    srand(1024);
    gettimeofday(&tvs,NULL);
    for(int i = 0; i < 1024*16 ; i++){
        for(int j = 0; j < 1024 ; j++){
            int key = rand()%tmax;
            if(tables[key]!=NULL){
                tables[key]=NULL;
            }
            tables[key]=(void *)1;
        }
    }
    gettimeofday(&tve,NULL);
    printf("rand = %ld msec ¥n",(tve.tv_sec-tvs.tv_sec)*1000+(tve.tv_usec - tvs.tv_usec)/1000);
    long diff = (tve.tv_sec-tvs.tv_sec)*1000+(tve.tv_usec - tvs.tv_usec)/1000;
    memset(tables,0,sizeof(tables));
    
    srand(1024);
    gettimeofday(&tvs,NULL);
    for(int i = 0; i < 1024*16 ; i++){
        for(int j = 0; j < 1024 ; j++){
            int key = rand()%tmax;
            if(tables[key]!=NULL){
                free(tables[key]);
            }
            tables[key]=malloc(packet);
        }
    }
    gettimeofday(&tve,NULL);
    printf("malloc = %ld msec ¥n",(tve.tv_sec-tvs.tv_sec)*1000+(tve.tv_usec - tvs.tv_usec)/1000-diff);

    for(int i = 0; i < tmax ; i++){
        if(tables[i]!=NULL){
            free(tables[i]);
            tables[i]=0;
        }
    }
    
    
    srand(1024);
    gettimeofday(&tvs,NULL);
    for(int i = 0; i < 1024 *16; i++){
        for(int j = 0; j < 1024 ; j++){
            int key = rand()%tmax;
            if(tables[key]!=NULL){
                dlfree(tables[key]);
            }
            tables[key]=dlmalloc(packet);
        }
    }
    gettimeofday(&tve,NULL);
    printf("dlmalloc = %ld msec ¥n",(tve.tv_sec-tvs.tv_sec)*1000+(tve.tv_usec - tvs.tv_usec)/1000-diff);
    
    for(int i = 0; i < tmax ; i++){
        if(tables[i]!=NULL){
            dlfree(tables[i]);
            tables[i]=0;
        }
    }

    srand(1024);
    gettimeofday(&tvs,NULL);
    for(int i = 0; i < 1024 *16; i++){
        for(int j = 0; j < 1024 ; j++){
            int key = rand()%tmax;
            if(tables[key]!=NULL){
                tc_free(tables[key]);
            }
            tables[key]=tc_malloc(packet);
        }
    }
    gettimeofday(&tve,NULL);
    printf("tc_malloc = %ld msec ¥n",(tve.tv_sec-tvs.tv_sec)*1000+(tve.tv_usec - tvs.tv_usec)/1000-diff);
    
    for(int i = 0; i < tmax ; i++){
        if(tables[i]!=NULL){
            tc_free(tables[i]);
            tables[i]=0;
        }
    }
    
    unsigned int tsize = 16*1024*1024;
    unsigned char *heap = malloc(tsize);
    mspace msp;
    msp = create_mspace_with_base(heap, tsize, 0);

    srand(1024);
    gettimeofday(&tvs,NULL);
    for(int i = 0; i < 1024 *16; i++){
        for(int j = 0; j < 1024 ; j++){
            int key = rand()%tmax;
            if(tables[key]!=NULL){
                mspace_free(msp,tables[key]);
            }
            tables[key]=mspace_malloc(msp,packet);
        }
    }
    gettimeofday(&tve,NULL);
    printf("mspace_malloc = %ld msec ¥n",(tve.tv_sec-tvs.tv_sec)*1000+(tve.tv_usec - tvs.tv_usec)/1000-diff);
    
    for(int i = 0; i < tmax ; i++){
        if(tables[i]!=NULL){
            mspace_free(msp,tables[i]);
            tables[i]=0;
        }
    }
    
    destroy_mspace(msp);    
    return 0;
}

